/*
 * @Author: your name
 * @Date: 2020-11-21 12:24:56
 * @LastEditTime: 2021-04-03 20:04:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \mongoDome\routes\src\addData.js
 */
var express = require('express');
var router = express.Router();
var db = require("../monmgoDB/db")
/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', { title: 'Express' });
});
router.post('/addListData', function (req, res, next) {
    /* 接收 title 辩题 message 简述 _id数据_id  content 详细内容 image 图片路径  createTime 创建时间*/
    let { title, message, content, image } = req.body,
        formNull = true;
    /* 判断req中是否有信息 防止for不执行 */
    if (req.body) {
        for (let key in req.body) {
            if (req.body[key] === "") {
                formNull = false
            }
        }
    } else {
        formNull = false
    }
    /* 判断表单信息完整 */
    if (formNull) {
        /* 生成创建时间 */
        var createTime = new Date().toLocaleString('chinese', { hour12: false }).replace(/\//g, "-");
        /* 创建数据库实例 传入保存数据 */
        var stu = new db({
            title, message, content, image, createTime
        })
        /* 调用数据库实例save方法进行保存 */
        stu.save(function (err, data) {
            if (!err) {
                res.send({
                    code: 200,
                    msg: "保存成功"
                })
            }
        })
    } else {
        /* 空判断不过 */
        res.send({
            code: 400,
            msg: "表单信息填写不完整！"
        })
    }
});

module.exports = router;